using DataTestFramework.NHibernate.Mappers;

using FluentNHibernate.Cfg;
using FluentNHibernate.Cfg.Db;
using FluentNHibernate.Mapping;

using NHibernate;
using NHibernate.Cfg;
using NHibernate.Mapping.ByCode;


namespace DataTestFramework.NHibernate
{
    public class NHibernateHelper
    {
        private readonly ISessionFactory _sessionFactory;

        public NHibernateHelper(string connectionString)
        {
            _sessionFactory = GetSessionFactory(connectionString);
        }

        private ISessionFactory GetSessionFactory(string connectionString)
        {
            //Configuration cfg = new Configuration().Configure(@"MSSQL.cfg.xml");
            //FluentConfiguration config = Fluently.Configure(cfg)
            //    .Mappings(m => m.FluentMappings.AddFromAssembly(GetType().Assembly)
            //    .ExportTo("d:\\"));
            //return Fluently.Configure()
            //    .Database(MsSqlConfiguration.MsSql2008.ConnectionString(m => m
            //        .Server(".")
            //        .Database("DataTestFramework")
            //        .TrustedConnection()))
            //    .Mappings(m => m
            //        .FluentMappings.AddFromAssembly(GetType().Assembly)
            //        .ExportTo("D:\\")).BuildSessionFactory();
            Configuration cfg = new Configuration().Configure();
            FluentConfiguration config = Fluently.Configure(cfg);
            config = config.Database(MsSqlConfiguration.MsSql2008.ConnectionString(connectionString));
                config = config.Mappings(m => m
                    .FluentMappings.AddFromAssembly(GetType().Assembly)
                    .ExportTo("D:\\"));
            return config.BuildSessionFactory();
        }

        public ISession GetSession()
        {
            return _sessionFactory.OpenSession();
        }
    }
}
